import 'package:dio/dio.dart';
import 'package:rabbit_mobile/utils/common_utils.dart';

/// 网络请求单例类
class XTXRequestManager {
  late final Dio _dio;

  static XTXRequestManager? _instance;

  XTXRequestManager._initManager() {
    BaseOptions baseOptions = BaseOptions(
      connectTimeout: const Duration(milliseconds: 150000),
      receiveTimeout: const Duration(milliseconds: 5000),
      baseUrl: 'http://pcapi-xiaotuxian-front.itheima.net/',
    );
    _dio = Dio(baseOptions);

    // 添加拦截器
    _dio.interceptors.add(LogInterceptor(requestBody: false, responseBody: false, error: true));
    _dio.interceptors.add(XTXInterceptors());
  }

  factory XTXRequestManager() {
    _instance ??= XTXRequestManager._initManager();
    return _instance!;
  }

  Future<Response> handleRequest(String path, String method, {data, Map<String, dynamic>? queryParameters}) async {
    try {
      return await _dio.request(
        path,
        data: data,
        queryParameters: queryParameters,
        options: Options(method: method),
      );
    } on DioException catch (e) {
      if (e.type == DioExceptionType.connectionTimeout) {
        throw Exception('Connection Timeout! Please check your network.');
      } else if (e.type == DioExceptionType.receiveTimeout) {
        throw Exception('Receive Timeout! Server may not be responding.');
      } else if (e.type == DioExceptionType.badResponse) {
        throw Exception('Server Error: ${e.response?.statusCode} -> ${e.response?.data}');
      } else if (e.type == DioExceptionType.unknown) {
        throw Exception('Network Error: ${e.message}');
      } else {
        throw Exception('Unexpected error: ${e.message}');
      }
    }
  }

  Future<Response> get(String path, {Map<String, dynamic>? queryParameters}) {
    return handleRequest(path, 'GET', queryParameters: queryParameters);
  }

  Future<Response> post(String path, {data, Map<String, dynamic>? queryParameters}) {
    return handleRequest(path, 'POST', data: data, queryParameters: queryParameters);
  }

  Future<Response> put(String path, {data, Map<String, dynamic>? queryParameters}) {
    return handleRequest(path, 'PUT', data: data, queryParameters: queryParameters);
  }

  Future<Response> delete(String path, {Map<String, dynamic>? queryParameters}) {
    return handleRequest(path, 'DELETE', queryParameters: queryParameters);
  }
}

/// 自定义拦截器
class XTXInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    final Map? userInfo = await userInfoGettingCache();
    final authorization = userInfo!.isNotEmpty ? 'Bearer ${userInfo['token']}' : '';
    options.headers = {
      'Authorization': authorization,
      'source-client': 'app',
    };
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    if (err.response?.statusCode == 401) {
      handler.reject(
        DioException(
          requestOptions: err.requestOptions,
          response: err.response,
          type: DioExceptionType.badResponse,
          error: 'unauthorized',
        ),
      );
    } else {
      super.onError(err, handler);
    }
  }
}
